
package com.scj.ho;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class MainActivity extends Activity {

    private TextView date = null;

    private TextView holiday = null;

    private ImageView view_one = null;

    private ImageView view_two = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
        bindEvent();
    }

    // 绑定事件
    private void bindEvent() {
        listView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Object object = parent.getAdapter().getItem(position);
                String name = getString(MyConstant.HOLIDAY_NAME_ID[0]);
                HolidayBean currentBean = null;
                if (object != null) {
                    currentBean = (HolidayBean) object;
                    name = currentBean.getName();
                }
                startBrowerActivity(name);
            }
        });
    }

    private void startBrowerActivity(String name) {
//        Toast.makeText(this, name, Toast.LENGTH_LONG).show();
        Intent intent=new Intent(this, BrowerPicActivity.class);
        intent.putExtra(MyConstant.KEY, name);
        startActivity(intent);
    }

    private void initialize() {
        ArrayList<HolidayBean> beans = HolidayUtil.getAllHoliday(this);
        listView = (ListView) findViewById(R.id.hol_list);
        listView.setAdapter(new MainActivityAdapter(this, beans));

        date = (TextView) findViewById(R.id.date);

        holiday = (TextView) findViewById(R.id.holiday);

        view_one = (ImageView) findViewById(R.id.ImageView_one);

        view_two = (ImageView) findViewById(R.id.ImageView_sec);

        HolidayBean currentBean = null;
        for (HolidayBean element : beans) {
            if (element.getNumber() > 0) {
                currentBean = element;
                break;
            }
        }

        if (currentBean == null) {
            date.setText("");
            holiday.setText("");
            view_one.setImageResource(R.drawable.num0);
            view_two.setImageResource(R.drawable.num0);
            return;
        }

        date.setText(currentBean.getDate());
        holiday.setText(String.format(getString(R.string.name_title), currentBean.getName()));
        int number = currentBean.getNumber();
        view_one.setImageResource(R.drawable.num0);
        view_two.setImageResource(R.drawable.num0);
        if (number >= 10) {
            number = number - 10;
            view_one.setImageResource(R.drawable.num1);
        } else if (number >= 20) {
            number = number - 20;
            view_one.setImageResource(R.drawable.num2);
        } else if (number >= 30) {
            number = number - 30;
            view_one.setImageResource(R.drawable.num3);
        }

        if (number >= 0 && number <= 9) {
            view_two.setImageResource(numberImage[number]);
        }
    }

    private ListView listView = null;

    private int[] numberImage = {
            R.drawable.num0, R.drawable.num1, R.drawable.num2, R.drawable.num3, R.drawable.num4,
            R.drawable.num5, R.drawable.num6, R.drawable.num7, R.drawable.num8, R.drawable.num9
    };
}
